function main_formatteddata(G,pid,sid,INDIR,OUTDIR)
%% Load Data (Formatted Raw, Formatted Data)
fprintf('%-6s %-6s %-20s Task (',pid,sid,'main_formatteddata');
indir=[G.DIR.DATA G.DIR.SEP INDIR];
infile=[pid '_' sid '_' G.FILE.FRMTRAW_MATNAME];
outdir=[G.DIR.DATA G.DIR.SEP OUTDIR];
outfile=[pid '_' sid '_' G.FILE.FRMTDATA_MATNAME];

load([indir G.DIR.SEP infile]);
if isempty(dir([outdir G.DIR.SEP outfile])) || G.RUN.FRMTDATA.LOADDATA==0, D=R;
else load([outdir G.DIR.SEP outfile]);end
%% Metadata
fprintf('metadata');
D.NAME=['FORMATEDDATA[' G.STUDYNAME ' ' pid ' ' sid ']'];


%% Load EMA
if G.RUN.FRMTDATA.EMA, fprintf('...ema');D.ema.data=format_EMA(R.ema.data);end
%% correct timestamp, interpolation
fprintf('...correct_timestamp');
for sensorid=G.RUN.FRMTDATA.SENSORLIST_CORRECTTIMESTAMP
%    fprintf([G.SENSOR.ID(sensorid).NAME ',']);
    D.sensor{sensorid}.sample=[];D.sensor{sensorid}.timestamp=[];D.sensor{sensorid}.matlabtime=[];
    timestamp=R.sensor{sensorid}.timestamp;
    sample=R.sensor{sensorid}.sample;
    for i=1:G.SAMPLE_TOS-1,        timestamp(i:G.SAMPLE_TOS:end)=0; end;
    segment = split_by_gaps(timestamp);
    if isempty(segment), continue; end
    [D.sensor{sensorid}.sample,D.sensor{sensorid}.timestamp] = correct_timestamps(G,sample,timestamp,segment,G.SENSOR.ID(sensorid).FREQ);
	D.sensor{sensorid}.matlabtime=convert_timestamp_matlabtimestamp(G,D.sensor{sensorid}.timestamp);    
end
%fprintf(')');
fprintf('...interpolate');
for sensorid=G.RUN.FRMTDATA.SENSORLIST_INTERPOLATE
%    fprintf([G.SENSOR.ID(sensorid).NAME ',']);
    
    timestamp=D.sensor{sensorid}.timestamp;
    sample=D.sensor{sensorid}.sample;
    [D.sensor{sensorid}.sample,D.sensor{sensorid}.timestamp] = interpolate_gaps(G,sample,timestamp,G.SENSOR.ID(sensorid).FREQ);
	D.sensor{sensorid}.matlabtime=convert_timestamp_matlabtimestamp(G,D.sensor{sensorid}.timestamp);
end

%% Data quality calculation: valid chunk, band off, band loose chunk
for sensorid=G.RUN.FRMTDATA.SENSORLIST_QUALITY
    if sensorid==G.SENSOR.R_RIPID
        D.sensor{sensorid}.quality=calculateDataQuality(G,D.sensor{sensorid}.sample,D.sensor{sensorid}.timestamp,G.SENSOR.R_RIPID,D.starttimestamp,D.endtimestamp);
        D.sensor{sensorid}.sample_all=D.sensor{sensorid}.sample;
        D.sensor{sensorid}.timestamp_all=D.sensor{sensorid}.timestamp;        
		D.sensor{sensorid}.matlabtime_all=D.sensor{sensorid}.matlabtime;
        [D.sensor{sensorid}.sample, D.sensor{sensorid}.timestamp,D.sensor{sensorid}.matlabtime]=saveGoodData(G,D.sensor{sensorid}.sample_all,D.sensor{sensorid}.timestamp_all,D.sensor{sensorid}.matlabtime_all,D.sensor{sensorid}.quality);
		fprintf('...dataquality_rip');
    elseif sensorid==G.SENSOR.R_ECGID
        D.sensor{sensorid}.quality=calculateDataQuality(G,D.sensor{sensorid}.sample,D.sensor{sensorid}.timestamp,G.SENSOR.R_ECGID,D.starttimestamp,D.endtimestamp);
        D.sensor{sensorid}.sample_all=D.sensor{sensorid}.sample;
        D.sensor{sensorid}.timestamp_all=D.sensor{sensorid}.timestamp;
        D.sensor{sensorid}.matlabtime_all=D.sensor{sensorid}.matlabtime;
        [D.sensor{sensorid}.sample, D.sensor{sensorid}.timestamp, D.sensor{sensorid}.matlabtime] = saveGoodData(G,D.sensor{sensorid}.sample_all,D.sensor{sensorid}.timestamp_all,D.sensor{sensorid}.matlabtime_all,D.sensor{sensorid}.quality);
        [D.sensor{sensorid}.sample, D.sensor{sensorid}.timestamp,D.sensor{sensorid}.matlabtime]=filter_bad_ecg(D.sensor{sensorid}.sample,D.sensor{sensorid}.timestamp,D.sensor{sensorid}.matlabtime);
%    	D.sensor{sensorid}.matlabtime=convert_timestamp_matlabtimestamp(G,D.sensor{sensorid}.timestamp);
        fprintf('...dataquality_ecg');
    end
end
%if RUN.FRMTDATA.NIDA_PDA_SELFREPORT, D.nida_pda_selfreport=read_nida_pda_selfreport(pid,sid,D);end;
%if RUN.FRMTDATA.JHU_PDA_LABREPORT,D.jhu_pda_labreport=read_jhu_pda_labreport(pid,D);disp('cocaineentry');end

%% Save Data
if isempty(dir(outdir))
    mkdir(outdir);
end
save([outdir G.DIR.SEP outfile],'D');
fprintf(') =>  done\n');

end
